package com.qifan.alb2b.action.front;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qifan.alb2b.action.common.PageController;
import com.qifan.alb2b.domain.CityInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.CityInfoService;
import com.qifan.alb2b.service.HotelInfoService;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

@Controller
@RequestMapping("/hotel")
public class HotelController extends PageController{
	private HotelInfoService hotelInfoService;

	public void setHotelInfoService(HotelInfoService hotelInfoService) {
		this.hotelInfoService = hotelInfoService;
	}
	
	private CityInfoService cityInfoService;

	public void setCityInfoService(CityInfoService cityInfoService) {
		this.cityInfoService = cityInfoService;
	}
	
	
	/**
	 * 根据酒店位置获得所有酒店列表
	 * @param request
	 * @param hotelLocation
	 * @param type
	 * @return
	 */
	@RequestMapping("/queryCityByLocation.usr")
	public String queryHotelByLocation(HttpServletRequest request, String location, String countryId){
		DataGridModel dgm=new DataGridModel();
		CityInfoDomain rqp=new CityInfoDomain();

		rqp.setCityType(Integer.parseInt(location));
		rqp.setCountryId(countryId);
		request.setAttribute("citys",cityInfoService.query(dgm, rqp).getRows());
		request.setAttribute("location",location);
				request.setAttribute("countryId",countryId);
		return "pages/front/hotel/city_hotel";
	}
	
	/**
	 * 根据酒店位置获得所有酒店列表
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryHotel.usr")
	public String queryHotel(HttpServletRequest request, String hotelId){
		request.setAttribute("hotels",hotelInfoService.getHotelById(hotelId));
		return "pages/front/hotel/hotelDetail";
	}
	
	/**
	 * 根据国家id查询所有的酒店位置
	 * @param request
	 * @param countryId
	 * @return
	 */
	@RequestMapping("/queryLocationByCountry.usr")
	public String queryLocationByCountry(HttpServletRequest request,String countryId,String countryCm){
		request.setAttribute("locations", cityInfoService.queryCityByCountry(countryId));
		request.setAttribute("countryCm",countryCm);
		request.setAttribute("countryId",countryId);
		return "pages/front/hotel";
	}
	
	@RequestMapping("/queryMoreCity.usr")
	public String queryMoreRoute(HttpServletRequest request,DataGridModel dgm,CityInfoDomain rqp,String location,String countryId){
		super.page(request, dgm);//调用父类的分页逻辑
		rqp.setCountryId(countryId);
		Page page= cityInfoService.query(dgm, rqp);
		request.setAttribute("page", page);
		request.setAttribute("rqp", rqp);
		return "pages/front/hotel/hotel_list";
	}
	@RequestMapping("/download.usr")
	public HttpServletResponse download(String path, HttpServletResponse response, HttpServletRequest request) { 
		String dirStr=request.getSession().getServletContext()
		.getRealPath("/upload")+"/";
		path=dirStr+path;
		System.out.println(path);
		try { 
		// path是指欲下载的文件的路径。 
		File file = new File(path); 
		if (!file.exists()) { 
			throw new BusinessException("文件不存在！");
			} 
		// 取得文件名。 
		String filename = file.getName(); 
		// 取得文件的后缀名。 
		//String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); 

		// 以流的形式下载文件。 
		InputStream fis = new BufferedInputStream(new FileInputStream(path)); 
		byte[] buffer = new byte[fis.available()]; 
		
		fis.read(buffer); 
		fis.close(); 
		// 清空response 
		response.reset(); 
		// 设置response的Header 
		response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); 
		response.addHeader("Content-Length", "" + file.length()); 
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); 
		response.setContentType("application/octet-stream"); 
		toClient.write(buffer); 
		toClient.flush(); 
		toClient.close(); 
		} catch (IOException ex) { 
		ex.printStackTrace(); 
		} 
		return response; 
		} 
}
